package com.liyuxiang.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyuxiang.yygh.cmn.client.DictFeignClient;
import com.liyuxiang.yygh.enums.DictEnum;
import com.liyuxiang.yygh.model.user.Patient;
import com.liyuxiang.yygh.user.mapper.PatientMapper;
import com.liyuxiang.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    DictFeignClient dictFeignClient;

    @Override
    public List<Patient> finAllByUserId(Long userId) {
        List<Patient> patientList = baseMapper.selectList(new LambdaQueryWrapper<Patient>().eq(Patient::getUserId, userId));
        patientList.stream().forEach(i -> {
            this.packPatient(i);
        });
        return patientList;
    }

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = packPatient(baseMapper.selectById(id));
        return patient;
    }

    public Patient packPatient(Patient patient) {
        String certificatesTypeString = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        String contactCertificatesTypeString = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        String provinceString = dictFeignClient.getDictName(patient.getProvinceCode());
        String cityString = dictFeignClient.getDictName(patient.getCityCode());
        String districtString = dictFeignClient.getDictName(patient.getDistrictCode());
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("certificatesTypeString", certificatesTypeString);
        paramMap.put("contactCertificatesTypeString", contactCertificatesTypeString);
        paramMap.put("provinceString", provinceString);
        paramMap.put("cityString", cityString);
        paramMap.put("districtString", districtString);
        patient.setCertificatesType(certificatesTypeString);
        patient.setParam(paramMap);
        return patient;
    }
}
